// pages/huashun/huashun.js
let name = ''
const app = getApp()
Page({
  data: {
    mod1: '章节1',
    mod2: '点击修改',
    pro: 0
  },
  //去添加新数据页
  add() {
    wx.navigateTo({
      url: '/pages/add/add',
    })
  },
  login_out(){
    wx.setStorageSync('name', "")
    wx.setStorageSync('password', '')
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  onLoad: function(options){
    console.log("进入onLoad")
    wx.stopPullDownRefresh()
    //从数据库下载信息
    wx.cloud.database().collection('studyRecords').skip(0)
    .where({
      name: app.globalData.name
    })
    .get()
    .then(res => {
      console.log("云函数查询数据库成功", res)
      let list = res.data
      list.forEach(item => {
        // console.log("item",item)
        let pro = this.suanfa(item)
        item.pro = pro
        // item.proTxt = Math.ceil(pro * 10)
      })
      console.log('list', list)
      this.setData({
        list: list
      })
      this.updateData(list)
    })
    .catch(res => {
      console.log("查询失败", res)
    })
    
  },
  onShow: function (options) {
    console.log("进入onShow")
      
      wx.showToast({
        title: '继续加油^v^',
        icon: 'success',
        duration: 2000
      });
      //返回界面重新渲染
      // this.onLoad();
      this.onLoad();
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      currentTab: 0 //当前页的一些初始数据，视业务需求而定
    })
    this.onLoad(); //重新加载onLoad()
    console.log("下拉刷新")
  },
  //触底刷新
  onReachBottom:function(){
    //获取数据列表的长度
    let x = this.data.list.length
    //判断数据列表的长度，超过20条刷新
    wx.cloud.database().collection('studyRecords').skip(x)
      .where({
        name: app.globalData.name
      })
      .get()
      .then(res => {
        console.log("云函数查询数据库成功", res)
        // let list = res.data
        let list1 = this.data.list
        list1.forEach(item => {
          // console.log("item",item)
          let pro = this.suanfa(item)
          item.pro = pro
          // item.proTxt = Math.ceil(pro * 10)
        })
        // console.log('list', list)
        this.setData({
          list: list1.concat(res.data)
        })
        let list2=this.data.list
        this.updateData(list2)
      })
      .catch(res => {
        console.log("查询失败", res)
      })
    if(x>20)
    {
      // wx.showLoading({
      //   title: '刷新中！',
      //   duration:4000
      // }) 
    }
  },
  // 算法部分
  suanfa(bean) {
    //先根据时间修改数据库再渲染
    // console.log("算法部分执行")
    var time = (new Date().getTime() / 1000 / 60 / 60 - 449300) - bean.oldTime1
    // console.log("time",time)
    var baeanPro = bean.pro1
    // console.log("baeanPro",baeanPro)
    var beanSum = bean.sum
    var beanfenshu =(bean.fenshu[9]+bean.fenshu[8])/2
    // console.log('baeanPro', baeanPro, "beanSum", beanSum)
    // console.log('baeanPro - 6', baeanPro - 6)
    //更新遗忘曲线规则
    for(var i=1;;i++)
    {
      if(beanSum>=20*(i-1)*(2-beanfenshu) && beanSum<20*i*(2-beanfenshu))
      {
        // console.log("baeanPro",baeanPro)
        var time0 = 2 / (Math.pow(baeanPro/10, i)) - 2
        //Math.pow(base,exponent)
        // console.log("time0",time0)
        // console.log("time0 + time",time0 + time)
        var pro = 1 / (Math.pow((time0 + time) / 2 + 1, 1 / i))
        // console.log("i= ",i)
        // console.log("pro ",pro)
        break
      }

    }
    // if (beanSum < 150) {
    //   var time0 = 6 / baeanPro - 6
    //   console.log('baeanPro - 6', baeanPro - 6)
    //   var pro = 6 / (time0 + time + 6)
    // } else if (150 < beanSum < 180) {
    //   var time0 = 6 / (Math.pow(baeanPro, 2)) - 6
    //   //Math.sqrt
    //   var pro = 1 / (Math.sqrt((time0 + time) / 6 + 1))
    // } else if (180 < beanSum < 200) {
    //   var time0 = 6 / (Math.pow(baeanPro, 3)) - 6
    //   //Math.pow(base,exponent)
    //   var pro = 1 / (Math.pow((time0 + time) / 6 + 1, 1 / 3))
    // } else if (200 < beanSum < 230) {
    //   var time0 = 6 / (Math.pow(baeanPro, 4)) - 6
    //   //Math.pow(base,exponent)
    //   var pro = 1 / (Math.pow((time0 + time) / 6 + 1, 1 / 4))
    // } else if (230 < beanSum < 250) {
    //   var time0 = 6 / (Math.pow(baeanPro, 5)) - 6
    //   //Math.pow(base,exponent)
    //   var pro = 1 / (Math.pow((time0 + time) / 6 + 1, 1 / 5))
    // } else if (250 < beanSum < 270) {
    //   var time0 = 6 / (Math.pow(baeanPro, 6)) - 6
    //   //Math.pow(base,exponent)
    //   var pro = 1 / (Math.pow((time0 + time) / 6 + 1, 1 / 6))
    // } else {
    //   var time0 = 6 / (Math.pow(baeanPro, 7)) - 6
    //   //Math.pow(base,exponent)
    //   var pro = 1 / (Math.pow((time0 + time) / 6 + 1, 1 / 7))
    // }
    // // return Math.ceil(pro * 100)
    // console.log('算法pro', pro)
    return pro
  },


  //修改时间和学习进度
  updateData(list) {
    if (list) {
      let promiseArr = []
      list.forEach(item => {
        // console.log('item.pro', item.pro)
        let promise = wx.cloud.database().collection('studyRecords')
          .doc(item._id)
          .update({
            data: {
              newTime1: new Date().getTime() / 1000 / 60 / 60 - 449300,
              oldTime1: new Date().getTime() / 1000 / 60 / 60 - 449300,
              pro: item.pro,
              pro1: item.pro*10,
              proTex: Math.ceil(item.pro*100)
            }
          })
        promiseArr.push(promise)
      })
      Promise.all(promiseArr).then(res => {
        console.log('全部更新得结果', res)
      }).catch(res => {
        console.log('更新失败', res)
      })
    }
  },
  //去修改当前进度或章节
  change(e) {
    wx.navigateTo({
      url: '/pages/huashun2/huashun2?id=' + e.currentTarget.dataset.id,
    })
  }
})







